import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart


def send_email(messages, subject, rec_email):
    msg = MIMEText(messages)
    msg['Subject'] = subject
    msg['From'] = 'lilenzhi@qq.com'
    msg['To'] = rec_email
    with smtplib.SMTP_SSL('smtp.qq.com', 465) as smtp:
        smtp.login('lilenzhi@qq.com', 'fcgpxfqboraleeej')
        smtp.send_message(msg)
        print('发送结束')


def send_email_with_image(messages, subject, rec_email, image_path):
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = 'lilenzhi@qq.com'
    msg['To'] = rec_email

    text_part = MIMEText(messages)
    msg.attach(text_part)

    with open(image_path, 'rb') as image_file:
        image_part = MIMEImage(image_file.read(), name='image.jpg')
    msg.attach(image_part)

    with smtplib.SMTP_SSL('smtp.qq.com', 465) as smtp:
        smtp.login('lilenzhi@qq.com', 'fcgpxfqboraleeej')
        smtp.send_message(msg)
        print('发送图片结束')


def send_email_files(messages, subject, rec_email, attachments):
    """

    :param messages:
    :param subject:
    :param rec_email:
    :return:
    """
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = 'lilenzhi@qq.com'
    msg['To'] = rec_email

    text_part = MIMEText(messages)
    msg.attach(text_part)

    for attachment in attachments:
        print(attachment)
        part = MIMEBase('application', 'octet-stream')
        with open(attachment, 'rb') as file:
            part.set_payload(file.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', f'attachment; filename="{os.path.basename(attachment)}"')
        msg.attach(part)

    with smtplib.SMTP_SSL('smtp.qq.com', 465) as smtp:
        smtp.login('lilenzhi@qq.com', 'fcgpxfqboraleeej')
        smtp.send_message(msg)
        print('发送图片结束')


def get_all_file():
    """

    :return:
    """
    file_list = []
    for i in os.walk("./output/"):
        for _ in i[2]:
            file_list.append(os.path.join(i[0], _))
    return file_list


if __name__ == '__main__':
    email_info = {"messages": "乖乖是傻瓜",
                  "subject": "傻瓜",
                  "rec_email": "llz15565161793@163.com",
                  'attachments': get_all_file()
                  }
    send_email_files(**email_info)

    os.getcwd()
    # print(get_all_file())

    # print(os.walk("./"))
